var vm = new Vue({
    el: '#main-block',
    data: function () {
        return {
            defaultTabName: 'addDomain',
            productClass: 'domain',
            /**
             * server List
             */
            hostList: [],
            /**
             * user List
             */
            userList: [],
            /**
             * domain products
             */
            productList: [],
            /**
             * domain status list
             */
            statusList: [{
                value: 'Y',
                name: '正常'
            }, {
                value: 'N',
                name: '暂停'
            }],
            /**
             * domain regType list
             */
            regTypeList: [],
            /**
             * vhost regType list
             */
            regTypeVhostList: [
                {
                    val:'vhost',
                    vname: '自建'
                },{
                    val:'baiduvhost',
                    vname: '百度云'
                }
            ],
            /**
             * 域名数据
             */
            newDomain: {
                userName: "",
                domainName: "",
                managepass: "",
                regType: 1,
                productCode: "",
                startTime: "",
                endTime: "",
                dns1: "",
                dns2: "",
                status: "Y",
                agentId: 0,
                remark: "手动添加数据"
            },
            /**
             * 域名初始化
             */
            initDomain: {
                userName: "",
                domainName: "",
                managepass: "",
                regType: 1,
                productCode: "",
                startTime: "",
                endTime: "",
                dns1: "",
                dns2: "",
                status: "Y",
                agentId: 0,
                remark: "手动添加数据"
            },
            /**
             * 虚拟主机数据
             */
            newVHost: {
                userAccount: "",
                webName: "",
                productCode: "",
                productName: "",
                startTime: "",
                endTime: "",
                webSiteName: "",
                webSiteServerCode: "",
                webFlow: 99,
                ftpName: "",
                ftpPwd: "",
                ftpServerCode: "",
                mysqlName: "",
                mysqlPwd: "",
                mysqlServerCode: "",
                mysqlSize: 999,
                sqlServerName: "",
                sqlPwd: "",
                sqlServerCode: "",
                remark: "手动添加数据"
            },
            /**
             * 虚拟主机初始化
             */
            initVHost: {
                userAccount: "",
                webName: "",
                productCode: "",
                productName: "",
                startTime: "",
                endTime: "",
                webSiteName: "",
                webSiteServerCode: "",
                webFlow: 99,
                ftpName: "",
                ftpPwd: "",
                ftpServerCode: "",
                mysqlName: "",
                mysqlPwd: "",
                mysqlServerCode: "",
                mysqlSize: 999,
                sqlServerName: "",
                sqlPwd: "",
                sqlServerCode: "",
                remark: "手动添加数据"
            },

            /**
             * 域名验证
             */
            domainrules: {
                userName: [
                    { required: true, message: '请输入会员名', trigger: 'blur' }
                ],
                domainName: [
                    { required: true, message: '请输入域名', trigger: 'blur' }
                ],
                managepass: [
                    { required: true, message: '请输入管理密码', trigger: 'blur' }
                ],
                productCode: [
                    { required: true, message: '请选择产品类型', trigger: 'change' }
                ],
                startTime: [
                    { type: 'date', required: true, message: '请选择开通时间', trigger: 'change' }
                ],
                endTime: [
                    { type: 'date', required: true, message: '请选择到期时间', trigger: 'change' }
                ],
                status: [
                    { required: true, message: '请选择产品状态', trigger: 'change' }
                ],
                regtype: [
                    { required: true, message: '请选择注册商', trigger: 'change' }
                ],
                dns1: [
                    { required: true, message: '请输入DNS1', trigger: 'blur' }
                ],
                dns2: [
                    { required: true, message: '请输入DNS2', trigger: 'blur' }
                ]
            },

            /**
             * 虚拟主机验证
             */
            vhostrules: {
                userAccount: [
                    { required: true, message: '请输入会员名', trigger: 'blur' }
                ],
                webName: [
                    { required: true, message: '请输入实例名称', trigger: 'blur' }
                ],
                managepass: [
                    { required: true, message: '请输入管理密码', trigger: 'blur' }
                ],
                productCode: [
                    { required: true, message: '请选择产品类型', trigger: 'change' }
                ],
                startTime: [
                    { type: 'date', required: true, message: '请选择开通时间', trigger: 'change' }
                ],
                endTime: [
                    { type: 'date', required: true, message: '请选择到期时间', trigger: 'change' }
                ],
                status: [
                    { required: true, message: '请选择产品状态', trigger: 'change' }
                ],
                regtype: [
                    { required: true, message: '请选择注册商', trigger: 'change' }
                ]
            }
        };
    },
    methods: {
        init: function(tab, event) {
            var self = this;
            self.productList = [];
            console.log(tab.name);
            if(tab.name == "addDomain"){
                self.newDomain.productCode = "";
                self.productClass = 'domain';
                self.domainInit();
            }else if(tab.name == "addVHost"){
                self.newVHost.productCode = "";
                self.productClass = 'vhost';
                self.vhostInit();
            }
            self.getProductList();
        },

        /**
         * 添加域名时加载初始数据
         */
        domainInit: function(){
            var self = this;
            self.getDomainRegTypes();
        },

        /**
         * 添加虚机时加载初始数据
         */
        vhostInit: function(){
            var self = this;
            self.getHostList();
        },

        /**
         * 域名添加.
         */
        addNewDomain: function () {
            var self = this;
            var valids = true;
            self.$refs['newDomain'].validate((valid) => {
                if (!valid) {
                valids = false;
            }
        });

            if (!valids){
                return;
            }

            var regType = parseInt(self.newDomain.regType);
            var productCode = self.newDomain.productCode;

            var regName = "";
            for (var i = 0; i < self.regTypeList.length; i++) {
                var item = self.regTypeList[i];
                if (item.regType == regType) {
                    regName = item.name;
                }
            }

            var productName = "";
            for (var i = 0; i < self.productList.length; i++) {
                var item = self.productList[i];
                if (item.productCode == productCode) {
                    productName = item.productName;
                }
            }

            if(self.newDomain.startTime >= self.newDomain.endTime){
                self.$message.error("到期时间不能大于开通时间");
                return;
            }

            var domian = {};
            domian.userAccount = self.newDomain.userName;
            domian.domain = self.newDomain.domainName;
            domian.managePass = self.newDomain.managepass;
            domian.regType = regType;
            domian.productCode = productCode;
            domian.startTime = self.newDomain.startTime;
            domian.endTime = self.newDomain.endTime;
            domian.dns1 = self.newDomain.dns1;
            domian.dns2 = self.newDomain.dns2;
            domian.status = self.newDomain.status;
            //domian.agentId = self.newDomain.agentId;
            domian.remark = self.newDomain.remark;
            domian.refer = 1;

            domian.regName = regName;
            domian.productName = productName;

            var domians = [];
            domians.push(domian);

            var request = {};
            request.domains = domians;
            var fn = "proInDomainSave";
            var service = Commonjs.jsonToString(request);
            var params = Commonjs.getParams(fn, service);
            Commonjs.ajaxSilence(weburl, params, true, function (response) {
                //console.log(JSON.stringify(response));
                var remark = response.data[0].remark;
                if(remark.indexOf("成功")){
                    self.$message.success(remark);
                }else {
                    self.$message.error(remark);
                }
            }, function (response) {
                // console.log(JSON.stringify(response));
                self.$message.error(response.msg);
            });
        },

        /**
         *  表单重置
         */
        resetForm: function(formName) {
            this.$refs[formName].resetFields();
        },

        /**
         * 虚机添加.
         */
        addNewVHost: function () {
            var self = this;
            var valids = true;
            self.$refs['newVHost'].validate((valid) => {
                if (!valid) {
                valids = false;
            }
        });

            if (!valids){
                return;
            }

            var regType = 'vhost';
            var productCode = self.newVHost.productCode;

            var regName = "";
            for (var i = 0; i < self.regTypeVhostList.length; i++) {
                var item = self.regTypeVhostList[i];
                if (item.regType == regType) {
                    regName = item.name;
                }
            }

            var productName = "";
            for (var i = 0; i < self.productList.length; i++) {
                var item = self.productList[i];
                if (item.productCode == productCode) {
                    productName = item.productName;
                }
            }

            if(self.newVHost.startTime >= self.newVHost.endTime){
                self.$message.error("到期时间不能大于开通时间");
                return;
            }


            var vhost = {};
            vhost.userAccount = self.newVHost.userAccount;
            vhost.webName = self.newVHost.webName;
            vhost.productCode = productCode;
            vhost.productName = productName;
            vhost.startTime = self.newVHost.startTime;
            vhost.endTime = self.newVHost.endTime;

            vhost.webSiteName = self.newVHost.webSiteName;
            vhost.webSiteServerCode = self.newVHost.webSiteServerCode;

            vhost.webFlow = 999;
            vhost.ftpName = self.newVHost.ftpName;
            vhost.ftpPwd = self.newVHost.ftpPwd;
            vhost.ftpServerCode = self.newVHost.ftpServerCode;

            vhost.mysqlSize = 2048;
            vhost.mysqlName = self.newVHost.mysqlName;
            vhost.mysqlPwd = self.newVHost.mysqlPwd;
            vhost.mysqlServerCode = self.newVHost.mysqlServerCode;

            vhost.sqlServerName = self.newVHost.sqlServerName;
            vhost.sqlPwd = self.newVHost.sqlPwd;
            vhost.sqlServerName = self.newVHost.sqlServerName;
            vhost.regType = regType;

            vhost.status = self.newVHost.status;
            vhost.remark = self.newVHost.remark;
            vhost.refer = 1;

            vhost.regName = regName;

            var vhosts = [];
            vhosts.push(vhost);

            var request = {};
            request.vHosts = vhosts;
            var fn = "proInVHostAdd";
            var service = Commonjs.jsonToString(request);
            var params = Commonjs.getParams(fn, service);
            Commonjs.ajaxSilence(weburl, params, true, function (response) {
                console.log(JSON.stringify(response));
                var remark = response.data[0].remark;
                if(remark.indexOf("成功")){
                    self.$message.success(remark);
                }else {
                    self.$message.error(remark);
                }

            }, function (response) {
                console.log(JSON.stringify(response));
                self.$message.error(response.msg);
            });
        },

        /**
         * 获取域名注册商
         */
        getDomainRegTypes: function(){
            var self = this;
            var service = {};
            var fn="queryUseRegType";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);//获取参数
            Commonjs.ajaxSilence(weburl, params, true, function (data) {
                if(data.result == "success"){
                    if (data.data.length>0){
                        var i = 0;
                        self.regTypeList = [];
                        BaseForeach(data.data,function(i,item){
                            var newItem = {};
                            newItem.val = parseInt(item.value);
                            newItem.lable = item.description;
                            self.regTypeList.push(newItem);
                            if (i==1){
                                self.newDomain.regType = item.value;
                            }
                            i= i+1;

                        });
                    }

                }
            }, function (response) {
            });
        },

        /**
         * 获取域名产品列表
         */
        getProductList: function(){
            var self = this;
            var service = {};
            service.productClass = self.productClass;
            service.pageSize = 999;
            if(self.productClass === 'vhost'){
                service.regType = self.productClass;
            }
            var fn="getProductList";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);//获取参数
            Commonjs.ajaxSilence(weburl, params, true, function (data) {
                if(data.result == "success"){
                    if (data.data.length > 0){
                        var i = 0;
                        self.productList= [];
                        BaseForeach(data.data,function(i,item){
                            var newItem = {};
                            newItem.productCode = item.productCode;
                            newItem.productName = item.productName;
                            self.productList.push(newItem);

                            i = i+1;

                        });
                    }

                }
            }, function (response) {
            });
        },

        /**
         * 获取会员列表
         */
        getUserList: function(){
            var self = this;
            var service = {};
            service.page = 1;
            service.pageSize = 99999;
            var fn="queryUser";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);//获取参数
            Commonjs.ajaxSilence(weburl, params, true, function (data) {
                if(data.result == "success"){
                    if (data.data.length > 0){
                        var i = 0;
                        self.userList= [];
                        BaseForeach(data.data,function(i,item){
                            if(item.status === "Y"){
                                var newItem = {};
                                newItem.userName = item.userName;
                                newItem.userId = item.userId;
                                newItem.lable = item.userId + " " + item.userName;
                                newItem.email = item.email;
                                self.userList.push(newItem);
                            }
                            i = i+1;
                        });
                    }
                }
            }, function (response) {
            });
        },

        /**
         * 获取服务器列表
         */
        getHostList: function(){
            var self = this;
            var service = {};
            service.hostType = "vhost";
            service.pagesize = 999;
            var fn="queryService";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);//获取参数
            Commonjs.ajaxSilence(weburl, params, true, function (data) {
                if(data.result == "success"){
                    if (data.data.length > 0){
                        var i = 0;
                        self.hostList= [];
                        BaseForeach(data.data,function(i,item){
                            var newItem = {};
                            newItem.hostId = item.hostId;
                            newItem.comIP = item.comIP;
                            self.hostList.push(newItem);

                            i = i+1;

                        });
                    }
                }
            }, function (response) {
            });
        },
        /**
         * 默认选中标签
         */
        setDefaultTabName: function(){
            var self = this;
            if(self.productClass === "domain"){
                self.defaultTabName = "addDomain";
            } else if (self.productClass === "vhost"){
                self.defaultTabName = "addVHost";
            } else if (self.productClass === "cloud"){
                self.defaultTabName = "addCloud";
            } else {
                self.defaultTabName = "addDomain";
            }
            self.getUserList();
        }
    },
    mounted: function () {
        var self = this;
        self.productClass = request("productClass");
        self.setDefaultTabName();
        self.domainInit();
        self.getProductList();
    }
});
